<template>
	<view class="flex-column container">
		<!-- <view class="search under-line padding-bottom-12" id="search" :style="searchStyle">
			<tian-search-bar :placeholder="$w('搜索商品')" bg_color="#f5f5f5" :disabled="true"
				@click="navTo('/pages/search/search')" style="padding-top:12rpx ;"></tian-search-bar>
		</view> -->
		<view class="" style="display: flex;justify-content: center;background-color: #eee;">
			<uv-tabs :list="topTabs" :current="0" @click="clickTabs"></uv-tabs>
		</view>
		<view class="main-category-panel-all ">
			<!-- 左侧主分类栏 -->
			<view class="left-menu-box">
				<scroll-view class="scroll-view_W" scroll-y="true" scroll-with-animation :scroll-top="mainCategoryTop">
					<template v-for="(item, index) in leftCategories" :key="index">
						<view @click="leftSelectChang(index)" class="item"
							:class="[currentLeftIndex == index? 'current':'']" :id="'main-'+item._id">
							<view class="text">
								<text>{{ item.name }}</text>
							</view>
						</view>
					</template>
				</scroll-view>
			</view>
			<view class="flex-1 content" :class="['main-index'+mainCategory.index]">
				<!-- 主分类图广告 -->
				<view class="ad-box">
					<uv-swiper height="75" :list="swiper_list_1" keyName="src" imgMode="scaleToFill" circular></uv-swiper>
				</view>
				<view class="sub-category">
					<!-- 二级分类导航 -->
					<scroll-view scroll-x="true" class="scroll-view_H" scroll-with-animation
						:scroll-left="subCategoryLeft">
						<label v-for="(item,index) in subCategories" :key="index">
							<view class="scroll-view-item_H" @click="selectSubcategory(item)"
								:id="'sub-left-' + item._id" :class="{current:item.selected}">
								<view class="nav-item-text">
									<text class="text">{{item.name}}</text>
								</view>
							</view>
						</label>

					</scroll-view>
				</view>
				<!-- 右侧分类的总scroll-view -->
				<scroll-view scroll-y="true" class="scroll-h3 padding-left-10" scroll-with-animation
					@scroll="subCategoryScroll" :scroll-top="subCategoryTop">
					<view class="third-category">
						<template v-for="(item,index) in subCategories" :key="index">
							<view class="item" :id="'sub-' + item._id">
								<view class="title" @click="loadList(item)">
									<text class="">{{item.name}}</text>
								</view>
								<!--  <wjf-goods-list v-if="item.children.length==0" v-model="item.cate2Goods"></wjf-goods-list> -->
								<!-- 三级分类 -->
								<view v-for="(item2,index2) in item.children" :key="index2" class="sub-category-3">
									<view class="category-3-title" @click="loadList(item2)">
										<view class="">
											<text>{{item2.name}}</text>
										</view>
										<view class="">
											<text style="font-size: 26tpx;color: #888;">更多</text>
											<uni-icons type="right" size="16" color="#777"></uni-icons>
										</view>
									</view>
									<wjf-goods-list :category_id="item2._id">
									</wjf-goods-list>
								</view>
							</view>
						</template>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- <tian-goods-sku :value="goods" ref="tianSkuPop"></tian-goods-sku> -->
		<!-- <view class="">
			<button type="default" @click="test">test</button>
		</view> -->
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCollection = db.collection('tian-mall-categories')
	const dbCmd = db.command
	// import tabbarMixin from './mixin/tabbar'
	import i18nMixin from './i18n/i18n-mixin.js'
	export default {
		mixins: [i18nMixin],
		data() {
			return {
				swiper_list_1: [],
				goods: {},
				mainCategory: {},
				topTabs: [], //顶部选项卡
				leftCategories: [],
				currentLeftIndex: 0,
				subCategories: [],
				subCateHeight: 100,
				subCategoryLeft: 0,
				subCategoryTop: 0,
				mainCategoryTop: 0,
				options: {
					scrollLeft: 0,
					scrollTop: 0,
					sv: "",
					mainCurrentId: "",
					subCurrentId: ""
				},
				key: 0
			};
		},
		onLoad(e) {
			if (e.id) {
				this.options.mainCurrentId = e.id;
			}
			this.loadFirstClass()
			// this.loadData()
		},
		computed: {
			goodsCondition() {
				return `category_id=="${this.options.subCurrentId}"`
			},
			searchStyle() {
				//#ifdef MP
				return `padding-top:${this.systemInfo.custom.top}px;`
				//#endif
				return ""
			}
		},
		onShow() {
			this.getSwiperImgs()
		},
		onHide() {
			//防止弹窗隐藏了导航栏
			uni.showTabBar()
			setTimeout(() => {
				this.$refs.tianSkuPop.close();
			}, 500)
		},
		methods: {
			async getSwiperImgs() {
				let where = `name == 'category_page_1' `
				let {
					result
				} = await db.collection('tian-banner-category').where(where).field('name,images').get();
				this.swiper_list_1 = result.data.filter(item => item.name == 'category_page_1')[0].images
				// this.swiper_list_2 = result.data.filter(item=>item.name=='home_page_2')[0].images
			},
			// 变更顶部选项卡
			clickTabs(e) {
				this.currentLeftIndex = 0
				this.getLeftClassData(e)
			},
			loadFirstClass() {
				dbCollection.where("status == true").orderBy('sort asc').field('name,sort,parent_id').get({
					getTree: {
						limitLevel: 1,
						startWith: "parent_id=='' || parent_id==null"
					}
				}).then(({
					result
				}) => {
					this.getLeftClassData(result.data[0])
					this.topTabs = result.data.map(item => {
						return {
							_id: item._id,
							name: item.name,
							sort: item.sort,
						}
					})
				})
			},
			/**
			 * 每次顶部选项卡变动时，会重新生成左侧数据，而左侧数据变动时则全部存储在数组中
			 * @param {Object} item
			 */
			getLeftClassData(item) {
				let where = `status == true && parent_id == '${item._id}'`
				dbCollection.where(where).orderBy('sort asc').field('name,sort,parent_id').get().then(({
					result
				}) => {
					this.leftCategories = result.data
					// 初始选取第0项
					this.getSecondClassData(0)
				})
			},
			// 选取左侧选项，如果未读分类数据，则添加相应的分类数据
			getSecondClassData(index) {
				if (!this.leftCategories[index].children) {
					dbCollection.where("status == true").orderBy('sort asc').field('name,sort,parent_id').get({
						getTree: {
							limitLevel: 3,
							startWith: `_id=="${this.leftCategories[index]._id}"`
						}
					}).then(({
						result
					}) => {
						this.leftCategories[index] = result.data[0]
						this.subCategories = this.leftCategories[index].children
						// 处理右侧二级分类
						if (this.subCategories.length > 0) {
							this.options.subCurrentId = this.subCategories[0]._id;
							this.$nextTick(() => {
								this.selectSubcategory(this.subCategories[0])
							})
						}
						setTimeout(() => {
							console.log('执行...');
							this.calcMainSize()
							this.calcSize()
						}, 10)
					})
				} else {
					this.subCategories = this.leftCategories[index].children
					// 处理右侧二级分类
					if (this.subCategories.length > 0) {
						this.options.subCurrentId = this.subCategories[0]._id;
						this.$nextTick(() => {
							this.selectSubcategory(this.subCategories[0])
						})
					}
					setTimeout(() => {
						console.log('执行...');
						this.calcMainSize()
						this.calcSize()
					}, 10)
				}


			},
			leftSelectChang(index) {
				this.currentLeftIndex = index
				this.getSecondClassData(index)
			},
			selectSubcategory(item) {
				console.log(item);
				//快速选择二级分类
				this.subCategories.forEach(cate => {
					cate.selected = item._id == cate._id;
				})
				this.subCategoryTop = item.top
			},
			subCategoryScroll(e) {
				//二级分类联动
				if (e.detail.scrollTop > 0) {
					this.subCategories.forEach(cate => {
						cate.selected = false;
						if (cate.top <= e.detail.scrollTop && cate.bottom > e.detail
							.scrollTop) {
							this.subCategoryLeft = cate.left
							cate.selected = true;
						}
					})
				} else {
					this.subCategoryLeft = 0
				}
			},
			loadList(item) {
				uni.navigateTo({
					url: "/pages/product/list?cid=" + item._id
				})
			},

			/**
			 * 列表上打开sku
			 * @param {Object} e
			 */
			openSku(e) {
				this.goods = e;
				this.$refs.tianSkuPop.open();
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				let left = 0;
				let marginLeft = 7;
				let preWidth = 0
				this.subCategories.forEach((item, index) => {
					let view = uni.createSelectorQuery().select('#sub-' + item._id);
					view.fields({
							size: true
						},
						data => {
							if (data) {
								item.top = h;
								h += data.height;
								item.bottom = h;
							}
						}
					).exec();

					//计算横向二级分类的宽度
					let viewLeft = uni.createSelectorQuery().select('#sub-left-' + item._id);
					viewLeft.fields({
							size: true
						},
						data => {
							item.left = left + marginLeft - preWidth;
							left += data.width + marginLeft;
							preWidth = data.width;
						}
					).exec();
				});

			},
			// 计算左侧高度
			calcMainSize() {
				let h = 0;
				//计算主分类的高度
				this.leftCategories.forEach(item => {
					let view = uni.createSelectorQuery().select('#main-' + item._id);

					view.fields({
							size: true
						},
						data => {
							if (data) {
								item.top = h;
								h += Math.round(data.height);
								item.bottom = h;
							}
						}
					).exec();
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #FFFFFF;
		height: 100%;
		width: 100%;
	}

	.search {
		padding-top: var(--status-bar-height);
		// #ifdef MP
		padding-top: 0;
		// #endif
		// background: #FFFFFF;
	}

	// $windowHeight:calc(100vh - (var(--window-top)) - (var(--window-bottom)) - var(--status-bar-height));

	.container {
		// height: $windowHeight;
		display: flex;
		height: 100%;
		width: 100%;
	}

	.container>view {
		width: 100%;
	}

	.main-category-panel-all {
		padding-top: 12rpx;
		flex: 1; //原代码 __wjf
		height: calc(100vh - (var(--window-top)) - (var(--window-bottom)) - var(--status-bar-height) - 110rpx);
		// #ifdef MP
		height: calc(100vh - (var(--window-top)) - (var(--window-bottom)) - var(--status-bar-height) - 134rpx);
		// #endif
		display: flex;
		width: 100%;
		height: 100%;
		overflow-x: hidden;


	}

	.left-menu-box {
		height: 100%;
	}

	.scroll-view_W {
		width: 160rpx; //修改过__wjf 原236
		height: 100%;
		text-align: center;
		// background: #FFFFFF;
		background: #f2f2f2;

		.item {
			height: 86rpx;
			padding: 0 4rpx;
			display: flex;
			text-align: left;
			align-items: center;
			justify-content: center;
			background: #f2f2f2;

			.text {
				font-size: 26rpx;
				line-height: 1.1;
				// word-break: break-all;
				text-align: center;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}
		}

		.current {
			font-weight: 600;
			background: #fff;
			color: deepskyblue;
		}

		.prev {
			border-radius: 0 0 16rpx 0;
			overflow: hidden;
		}

		.next {
			border-radius: 0 16rpx 0 0;
			overflow: hidden;
		}
	}

	.content {
		background: #FFFFFF;
		padding-top: 30rpx;
		border-radius: 30rpx 0 0 0;
		height: calc(100% - 30rpx);
		width: calc(100vw - 160rpx);

		&.main-index0 {
			border-radius: 0;
		}

		.ad-box {
			width: calc(100vw - 200rpx);
			height: 150rpx;
			margin-left: 22rpx;
			/* #ifdef MP-WEIXIN */
			margin-bottom: 30rpx;
			/* #endif */


		}
	}

	// 右侧二级导航部分
	.sub-category {
		width: 580rpx;
		height: 80rpx;
		// padding: 10rpx 0;
		margin-left: 6rpx;
		// background-color: #008080;
		display: flex;
		overflow: hidden;
		justify-content: center;
		align-items: center;

		.scroll-view-item_H {
			// padding-left: $uni-spacing-row-base;

			&:last-child {
				margin-right: $uni-spacing-row-base;
			}

			&.current {
				.text {
					// background: $uni-red;
					color: $uni-red;
					font-weight: 600;
					// border-radius: 12rpx;
				}
			}

			height: 100%;

			.nav-item-text {
				display: flex;
				// flex-wrap: wrap;
				word-break: break-all;
				height: 70rpx;
				// line-height: 1.1;
				align-items: center;
				text-align: center;
				// width: 100%;
				// height: 100%;
				background: $uni-bg-color-grey;

				.text {
					white-space: normal;
					// word-break:break-all;
					// background: $uni-bg-color-grey;
					font-size: 26rpx;
					line-height: 1.1;
					// height: 52rpx;
					max-width: 160rpx;
					border-radius: 14rpx;
					margin-left: 16rpx;
					padding: 10rpx 16rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					text-overflow: ellipsis;
				}

			}
		}

		.item {
			// display: inline-flex;
			// height: 68rpx;
			// line-height: 1.1;
			// align-items: center;
			// justify-content: center;
			// height: 86rpx;
			// text-align: center;

			&.current {

				// background: $uni-bg-color;
				.text {
					color: $uni-red;
					font-weight: 600;
				}

			}

			.nav-item-text {
				display: flex;
				height: 70rpx;
				line-height: 1.1;
				// align-items: center;
				text-align: left;
				width: 100%;
				// height: 100%;
				background: $uni-bg-color-grey;

				.text {
					white-space: normal;
					// word-break:break-all;
					// background: $uni-bg-color-grey;
					font-size: 26rpx;
					// line-height: 1.1;
					// height: 52rpx;
					max-width: 160rpx;
					border-radius: 14rpx;
					margin-left: 16rpx;
					padding: 10rpx 16rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}
		}

	}

	.scroll-h3 {
		height: calc(100% - 230rpx);
		width: 100%;
	}

	.third-category {
		padding-bottom: 60rpx;

		.item {}

		.title {
			font-weight: 600;
			padding: 14rpx 0;
			display: flex;
			justify-content: center;
			color: #EE4000;
		}

		.sub-items {
			display: flex;
			flex-wrap: wrap;
			text-align: center;
			font-size: 24rpx;
			// justify-content: space-between;
			padding-right: 30rpx;

			.sub-item {
				width: 33.33%;
			}
		}

		.image {
			width: 133rpx;
			height: 133rpx;
			display: block;
			margin: 6rpx auto;
		}

		// 三级分类总
		.sub-category-3 {
			background-color: #eee;
			border-radius: 6rpx;
			margin: 8rpx 0;
			padding-left: 6rpx;

			.category-3-title {
				width: calc(100% - 30rpx);
				padding: 12rpx 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				text {
					color: #008080;
					font-size: 26rpx;
				}

			}

			.category-3-box {
				display: flex;
				flex-wrap: wrap;
				// background-color: #eee;
				padding: 10rpx 0;

				//四级分类块
				.sub-category-4 {
					margin: 10rpx;
					padding: 6rpx;
					display: flex;
					flex-wrap: wrap;
					max-width: 30%;
					line-height: 1.1;
					color: #555;
					font-size: 24rpx;
					align-items: center;
					border: 1px solid #aaa;
					border-radius: 12rpx;
				}
			}
		}

	}

	.goods-list-panel {
		width: 100%;
		height: 100%;
		flex: 1;
	}
</style>